package com.cysion.study.ui.fragment

import android.support.v7.widget.LinearLayoutManager
import android.view.View
import com.cysion.other.startActivity_ex
import com.cysion.study.R
import com.cysion.study.adapter.LeveManageAdapter
import com.cysion.study.adapter.LeveManageAdapter.Companion.look
import com.cysion.study.entity.GradingListEntity
import com.cysion.study.entity.Infolist
import com.cysion.study.helper.UserCache
import com.cysion.study.ktbox.base.BaseFragment
import com.cysion.study.ktbox.net.BaseResponseRx
import com.cysion.study.ktbox.net.ErrorHandler
import com.cysion.study.net.ApiClient
import com.cysion.study.ui.LeveCommentActivity
import com.cysion.study.uibox.dialog.Alert
import com.cysion.study.widget.ShowToast
import com.cysion.targetfun._subscribe
import com.scwang.smartrefresh.layout.constant.RefreshState
import kotlinx.android.synthetic.main.fragment_leve_manage_ok.*

/**
 * 考级管理
 * Created by chu on 2019-06-30.
 */
class LeveManageNoFragment : BaseFragment(), View.OnClickListener {

    private lateinit var classAdapter: LeveManageAdapter
    //    private var mSourceList: MutableList<String> = mutableListOf()
    override fun getLayoutId(): Int {
        return R.layout.fragment_leve_manage_ok
    }

    override fun initView() {
        initRecyclerView()
        initRefreshLayout()
    }

    var type = "0"
    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.tab1 -> {//
                type = "0"
            }
            R.id.tab2 -> {
                type = "1"
            }
        }
    }

    private var curPage = 1
    private fun initRecyclerView() {
        rv_list.isNestedScrollingEnabled = false
        classAdapter = LeveManageAdapter(dataList, context)
        rv_list.adapter = classAdapter
        rv_list.layoutManager = LinearLayoutManager(context)

        classAdapter.setOnTypeClickListener { obj, position, flag ->
            if (flag == look) {
                context.startActivity_ex<LeveCommentActivity>()//去考核
            }
        }

    }

    //    初始化刷新控件
    private fun initRefreshLayout() {
        smartLayout.setOnRefreshListener {
            curPage = 1
            getDataList()
            smartLayout.setEnableLoadMore(true)

        }
        smartLayout.setOnLoadMoreListener {
            getDataList()
        }
    }

    override fun initData() {
        super.initData()
        smartLayout.autoRefresh()
    }

    private val dataList: MutableList<Infolist> = mutableListOf()

    private fun getDataList() {
        Alert.loading(activity!!)
        ApiClient.api.gradingList("", "", "", "", "", "", "", UserCache.token)
            .compose(BaseResponseRx.validateToMain())
            ._subscribe {
                _onNext {
                    Alert.close()
                    stopRefresh()
                    dataList.clear()
                    if (dataList.size == 0) {
                        multiView.showEmpty()
                    } else {
                        dataList.addAll(it.infolist)
                        classAdapter.notifyDataSetChanged()
                        multiView.showContent()
                        smartLayout.setEnableLoadMore(false)
                    }

                    /*if (curPage == 1) {
                        dataList.clear()
                        if (dataList.size == 0) {
                            multiView.showEmpty()
                        }
                    }
                    if (it.size <= 0) {
                        smartLayout.setEnableLoadMore(false)
                    } else {
                        curPage++
                        dataList.addAll(it)
                        classAdapter.notifyDataSetChanged()
                        multiView.showContent()
                    }*/
                }
                _onError {
                    Alert.close()
                    ShowToast(ErrorHandler.handle(it).errorMsg)
                    stopRefresh()
                }
            }
    }


    private fun stopRefresh() {
        if (smartLayout.state == RefreshState.Refreshing) {
            smartLayout.finishRefresh()
        } else if (smartLayout.state == RefreshState.Loading) {
            smartLayout.finishLoadMore(100)
        }
    }


    override fun closeMvp() {
    }
}